A PROGRAM AND A PROBLEM: \\

BY FRANCIS NAISH.\\

As a relative newcomer to SmartBASIC programming, I 
only  work with short programs. I have a P.D. sorting 
program, ALPHASORT, that I use to alphabetically sort my 
taped computer games, along with their identifying datapack. 
 This utility will  save the sorted data but will not 
recover the data to make additions. My objective was to 
modify ALPHASORT to do this. This  was my first experience in 
saving  and recovering files internally. First, I 
studied some examples.
Initially my attempted modifications did not work, so I 
started to assemble component sub-programs, one at a time.  
Eventually I was successful and the result is ASORT, as listed 
below.

The problem?  Well, apart from many of my own making, there 
was one that was particularly intriguing.  At one late 
stage, I had the program running when MON(itored) I,O,C but it 
would not run  when NOMON(itored) I,O,C. (See page A55 of 
Adam SmartBASIC Manual). At this point I turned for help 
to Michael Lyons, our PD Librarian, and an experienced 
SmartBASIC programmer, who searched his memory and suggested 
trying to close the file twice. (See lines 150 and 152 also 
lines 235 and 237). And it worked! Why??

Strangely enough, two weeks later I received a copy of a 
1984 issue of a now defunct newsletter (from Norman Castro) 
and here was the same problem explained.  The solution that 
was given, was to HOME before closing (change lines 150 and 
235 to HOME).  I can't say that I understand the reason, 
which involves the screen buffer, however some readers may 
find this problem interesting.   (I leave my original program 
for you to change if you wish.  Try  running it both NOMON and 
MON I,O,C.)

Hope you find it enjoyable and useful.
   5 REM ASORT by  F.Naish (6/90)
  10 GOSUB 900
  25 DIM item$(m): d$=CHR$(4)
  30 GOTO 1000
  35 REM  list,sort,print
  40 count=1
  45 IF count<1 THEN  count=1
  47 IF count>m THEN ? "No more items": count=count-1:GOTO 70
  50 ? "Item # "; count; :INPUT "?"; item$(count)
  55 IF item$(count)=CHR$(99) OR item$(count)=CHR$(67) 
     THEN count=count-1:GOTO 45
  60 IF item$(count)="" THEN count=count-1:GOTO 70
  65 count=count+1:GOTO 47
  70 ?:INPUT "Sort Alphabetically? (y/n)  "; a$
  75 IF a$="y" OR a$="Y" THEN GOSUB 400
  80 ?:? "Print & View List? (y/n) "
  85 INPUT "(Paper in Printer?)"; p$
  90 IF p$="y" OR p$="Y" THEN GOSUB 750
 100 REM    write file
 105 ? d$; "open "; name$; " ,D"; d%
 110 ? d$; "write "; name$
 115 ? count
 120 FOR listing=1 TO (count)
 125 ? item$(listing)
 130 NEXT
 150 ? d$; "close "; name$
 152 ? d$; "close "; name$
 155 GOTO 1000
 200 REM   read file
 205 ? d$; "open "; name$; " ,D"; d%
 210 ? d$; "read "; name$
 215 INPUT count: num=count
 220 FOR num=1 TO count
 225 INPUT item$(num)
 230 NEXT
 235 ? d$; "close "; name$
 237 ? d$; "close "; name$
 240 REM  add to file
 245 IF num<1 THEN  num=1
 250 IF num>m THEN ? "No more items": num=num-1:GOTO 275
 255 ? "Item # "; num; :INPUT "?"; item$(num)
 260 IF item$(num)=CHR$(99) OR item$(num)=CHR$(67) THEN 
      num=num-1:GOTO 245\\
 265 IF item$(num)="" THEN  num=num-1:GOTO 275
 270 num=num+1:GOTO 250
 275 count=num
 280 GOTO 70
 400 ? "Now sorting your "; count; " items"
 450 guide=1
 455 guide=2*guide:IF guide<count GOTO 455
 460 guide=INT((guide-1)/2):IF guide=0 THEN GOTO 500
 465 FOR i=1 TO count-guide: j=i
 470 k=j+guide:IF item$(k)<item$(j) THEN  q$=item$(j): item$
     (j)=item$(k): item$(k)=q$: j=j-guide:IF j>0 GOTO 470 
 475 NEXT:GOTO 460
 500 ? "Sorting Complete":RETURN
 750 PR #1
 755 FOR listing=1 TO count
 760 ? item$(listing)
 765 NEXT:PR #0:RETURN
 900 HOME:?:? "A Utility to File and"
 905 ? "Add To a Single List,"
 910 ? "Alphabetically, if required."
 915 ? "Files may be Printed."
 920 ?:INPUT "File Name?  "; name$
 925 ?:?:INPUT "Maximum Number of Items?  "; m
 930 ?:INPUT "To/From Drive No.  "; d%
 935 ?:?:? "INSTRUCTIONS:"
 940 ? "Press <Return> to end list."
 945 ? "<c>,<Ret> cancels last item."
 950 ?:INPUT "Are you ready? (y/n)  "; q$
 955 IF q$="y" OR q$="Y" THEN 25
 960 GOTO 950
 965 RETURN
 1000 HOME:? "OPTIONS"
 1005 ? "a) Enter Items & File"
 1010 ? "b) Add More Items to Same File"
 1015 ? "   (To print saved file ---"
 1017 ? "              --- add 0 items)"
 1020 ? "c) Exit (or Rerun)"
 1100 ?:INPUT "What Option?  "; op$
 1105 IF op$="a" THEN GOTO 40
 1110 IF op$="b" THEN GOTO 200
 1115 IF op$="c" THEN GOTO 1200
 1120 IF op$<>"a" OR op$<>"b" OR op$<>"c" THEN GOTO 1100
 1200 ?:? "END":END

